var ws;
$(function(){
    link();
})

function link () {
    ws = new WebSocket("ws://106.12.201.205:9501");//连接服务器
    ws.onopen = function(event){
        //console.log(event);
        alert('连接了');
        sendIdentity();
    };
    ws.onmessage = function (event) {
        //console.log(event.data);return;
        var data = JSON.parse(event.data);
        console.log(data);

        localStorage.setItem('ws_guest_id', data['userFd']);
        localStorage.setItem('ws_guest_to_id', data['toUserFd']);
        localStorage.setItem('ws_guest_chat_id', data['chat_id']);
        var myInfo = '我是' + data['userFd'] + '号（游客）';
        if (data['toUserFd']) {
            userInfo = '我在和' + data['toUserFd'] + '号(客服)聊天';
        } else {
            userInfo = '当前没有客服在线';
        }
        $("#myInfo").text(myInfo);
        $("#userInfo").text(userInfo);
        if (data['type'] == 3) {
            return;
        }
        var show_msg = replace_em(data['msg']);
        if (data['type'] == 6) {
            show_msg = "<a download="+data['msg']+" href='" + data['url'] + "'>" + show_msg + "</a>"
        }
        console.log(data);
        var msg = "<div class=\"pnl-list\" id=\"msgs\">\n" +
            "                                <div class=\"msg robot\" id=\"welcome\">\n" +
            "                                    <div class=\"msg-left\" id=\"welcomeLeft\">\n" +
            "                                        <div class=\"msg-host photo\" id=\"welcomeIcon\" style=\"background-image: url(&quot;https://www.v5kf.com/files/icons/201610/14761555537.png&quot;);\"></div>\n" +
            "                                        <div class=\"msg-ball\" id=\"welcomeBall\"><span>" + show_msg + "</span></div>\n" +
            "                                    </div>\n" +
            "                                </div>";
        $("#show").append(msg);
        // var scrollTop = $("#msgArea")[0].scrollHeight;
        // $("#msgArea").scrollTop(scrollTop);
    }
    ws.onclose = function(event){alert("已经与服务器断开连接\r\n当前连接状态："+this.readyState);};

    ws.onerror = function(event){
        alert("请登录后重新打开对话框！");
    };
}

function sendMsg(){
    var msg = $("#text").val();
    var data = {type:2,userFd:localStorage.getItem('ws_guest_id'),toUserFd:localStorage.getItem('ws_guest_to_id'),msg:msg,chat_id:localStorage.getItem('ws_guest_chat_id')};
    $("#text").val('');
    var msg = "<div class=\"msg guest\" id=\"1543389702033-0\">\n" +
        "                                <div class=\"msg-right\">\n" +
        "                                    <div class=\"msg-host\"><i class=\"ic kh\"></i></div>\n" +
        "                                    <div class=\"msg-ball\" title=\"今天 15:38:29\">"+replace_em(msg)+"</div>\n" +
        "                                </div>\n" +
        "                            </div>";
    $("#show").append(msg);
    ws.send(JSON.stringify(data));
}

function replace_em(str) {
    str = str.replace(/\</g, '&lt;');
    str = str.replace(/\>/g, '&gt;');
    str = str.replace(/\n/g, '<br/>');
    str = str.replace(/\[:([\s\S]+?)\]/g, '<img src="gif/$1.gif" border="0" />');
    str = str.replace(/!\[([\s\S]+?)\]/g, '<img src="$1" border="0" />');
    return str;
}

function sendIdentity() {
    var data = {type:3,userFd:localStorage.getItem('ws_guest_id'),identity_type:1,toUserFd:localStorage.getItem('ws_guest_to_id')};
    ws.send(JSON.stringify(data));
}